{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2023 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# golomb8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/golomb8.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/golomb8.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "This is the Golomb ruler problem.\n",
    "\n",
    "This model aims at maximizing radar interferences in a minimum space.\n",
    "It is known as the Golomb Ruler problem.\n",
    "\n",
    "The idea is to put marks on a rule such that all differences\n",
    "between all marks are all different. The objective is to minimize the length\n",
    "of the rule.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.sat.colab import flags\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "FLAGS = flags.FLAGS\n",
    "\n",
    "# We disable the following warning because it is a false positive on constraints\n",
    "# like: solver.Add(x == 0)\n",
    "# pylint: disable=g-explicit-bool-comparison\n",
    "\n",
    "\n",
    "def main(_):\n",
    "    # Create the solver.\n",
    "    solver = pywrapcp.Solver(\"golomb ruler\")\n",
    "\n",
    "    size = 8\n",
    "    var_max = size * size\n",
    "    all_vars = list(range(0, size))\n",
    "\n",
    "    marks = [solver.IntVar(0, var_max, \"marks_%d\" % i) for i in all_vars]\n",
    "\n",
    "    objective = solver.Minimize(marks[size - 1], 1)\n",
    "\n",
    "    solver.Add(marks[0] == 0)\n",
    "\n",
    "    # We expand the creation of the diff array to avoid a pylint warning.\n",
    "    diffs = []\n",
    "    for i in range(size - 1):\n",
    "        for j in range(i + 1, size):\n",
    "            diffs.append(marks[j] - marks[i])\n",
    "    solver.Add(solver.AllDifferent(diffs))\n",
    "\n",
    "    solver.Add(marks[size - 1] - marks[size - 2] > marks[1] - marks[0])\n",
    "    for i in range(size - 2):\n",
    "        solver.Add(marks[i + 1] > marks[i])\n",
    "\n",
    "    solution = solver.Assignment()\n",
    "    solution.Add(marks[size - 1])\n",
    "    collector = solver.AllSolutionCollector(solution)\n",
    "\n",
    "    solver.Solve(\n",
    "        solver.Phase(marks, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE),\n",
    "        [objective, collector],\n",
    "    )\n",
    "    for i in range(0, collector.SolutionCount()):\n",
    "        obj_value = collector.Value(i, marks[size - 1])\n",
    "        time = collector.WallTime(i)\n",
    "        branches = collector.Branches(i)\n",
    "        failures = collector.Failures(i)\n",
    "        print(\n",
    "            (\"Solution #%i: value = %i, failures = %i, branches = %i,\" \"time = %i ms\")\n",
    "            % (i, obj_value, failures, branches, time)\n",
    "        )\n",
    "    time = solver.WallTime()\n",
    "    branches = solver.Branches()\n",
    "    failures = solver.Failures()\n",
    "    print(\n",
    "        (\n",
    "            \"Total run : failures = %i, branches = %i, time = %i ms\"\n",
    "            % (failures, branches, time)\n",
    "        )\n",
    "    )\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
